package cn.kmsoft.jssc.entity;

import java.io.Serializable;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author : cqwensten
 * @description : todo
 * @email : cqwensten@163.com
 * @date : 2021/11/18
 */
public class ServerUrl implements Serializable {
    private String url;
    private boolean matchAll;
    private String oauth2Endpoint;
    private String username;
    private String password;
    private String clientId;
    private String clientSecret;
    private String rateLimit;
    private String rateLimitPeriod;
    private String tokenServiceUri;
    private String hostRedirect;
    private static ConcurrentHashMap<String, String> tokenServiceMap = new ConcurrentHashMap<String, String>();

    public ServerUrl(String url) {
        this.url = url;
    }

    public ServerUrl(String url, String matchAll, String oauth2Endpoint, String username, String password, String clientId, String clientSecret, String rateLimit,
                     String rateLimitPeriod, String tokenServiceUri, String hostRedirect) {

        this.url = url;
        this.matchAll = matchAll == null || matchAll.isEmpty() || Boolean.parseBoolean(matchAll);
        this.oauth2Endpoint = oauth2Endpoint;
        this.username = username;
        this.password = password;
        this.clientId = clientId;
        this.clientSecret = clientSecret;
        this.rateLimit = rateLimit;
        this.rateLimitPeriod = rateLimitPeriod;
        this.tokenServiceUri = tokenServiceUri;
        this.hostRedirect = hostRedirect;
    }


    public String getUrl() {
        return this.url;
    }

    public void setUrl(String value) {
        this.url = value;
    }

    public boolean getMatchAll() {
        return this.matchAll;
    }

    public void setMatchAll(boolean value) {
        this.matchAll = value;
    }

    public String getHostRedirect() {
        return hostRedirect;
    }

    public void setHostRedirect(String hostRedirect) {
        this.hostRedirect = hostRedirect;
    }

    public String getOAuth2Endpoint() {
        return this.oauth2Endpoint;
    }

    public void setOAuth2Endpoint(String value) {
        this.oauth2Endpoint = value;
    }

    public String getUsername() {
        return this.username;
    }

    public void setUsername(String value) {
        this.username = value;
    }

    public String getPassword() {
        return this.password;
    }

    public void setPassword(String value) {
        this.password = value;
    }

    public String getClientId() {
        return this.clientId;
    }

    public void setClientId(String value) {
        this.clientId = value;
    }

    public String getClientSecret() {
        return this.clientSecret;
    }

    public void setClientSecret(String value) {
        this.clientSecret = value;
    }

    public int getRateLimit() {
        return (this.rateLimit == null || this.rateLimit.isEmpty()) ? -1 : Integer.parseInt(this.rateLimit);
    }

    public void setRateLimit(int value) {
        this.rateLimit = String.valueOf(value);
    }

    public int getRateLimitPeriod() {
        return (this.rateLimitPeriod == null || this.rateLimitPeriod.isEmpty()) ? -1 : Integer.parseInt(this.rateLimitPeriod);
    }

    public void setRateLimitPeriod(int value) {
        this.rateLimitPeriod = String.valueOf(value);
    }

    public String getTokenServiceUri() {
        if (this.tokenServiceUri == null && tokenServiceMap != null) {
            this.tokenServiceUri = tokenServiceMap.get(this.url);
        }
        return this.tokenServiceUri;
    }

    public void setTokenServiceUri(String value) {

        this.tokenServiceUri = value;

        tokenServiceMap.put(this.url, value);
    }

    @Override
    public String toString() {
        return "ServerUrl{" +
                "url='" + url + '\'' +
                ", matchAll=" + matchAll +
                ", oauth2Endpoint='" + oauth2Endpoint + '\'' +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", clientId='" + clientId + '\'' +
                ", clientSecret='" + clientSecret + '\'' +
                ", rateLimit='" + rateLimit + '\'' +
                ", rateLimitPeriod='" + rateLimitPeriod + '\'' +
                ", tokenServiceUri='" + tokenServiceUri + '\'' +
                ", hostRedirect='" + hostRedirect + '\'' +
                '}';
    }
}
